<?php
/**
 * CakeCMS - 网站AJAX操作
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */

class AjaxController extends AppController {
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// Ajax里不使用模板
		$this->autoRender = false;
	}
	
	/**
	 * 获取城市
	 */
	public function getCity() {
		$province = $this->data ['province'];
		
		if (empty ( $province ) || $province == 0) {
			echo "<option value=\"0\">请选择</option>";
			exit ();
		} else {
			$this->loadModel ( "City" );
			$conditions = array (
					"pid" => $province 
			);
			
			$CityList = $this->City->GetCityByList ( $conditions );
			if ($CityList) {
				$html = "<option value=\"0\">请选择</option>";
				
				$this->loadModel ( "User" );
				$UserInfo = $this->User->GetUserByID ( $this->Cookie->read ( 'uid' ) );
				
				foreach ( $CityList as $val ) {
					$is_selected = ($val ['City'] ['id'] == $UserInfo ['User'] ['city']) ? "selected=\"selected\"" : "";
					$html .= "<option {$is_selected} value=\"{$val['City']['id']}\">{$val['City']['name']}</option>";
				}
				echo $html;
				exit ();
			} else {
				echo "<option value=\"0\">请选择</option>";
				exit ();
			}
		}
	}
	
	/**
	 * 设置区
	 */
	public function getArea() {
		
		$city = $this->data ['city'];
		$area = $this->data ['area'];
		
		if (empty ( $city ) || $city == 0) {
			echo "<option value=\"0\">请选择</option>";
			exit ();
		} else {
			$this->loadModel ( "City" );
			$conditions = array (
					"pid" => $city 
			);
			
			$CityList = $this->City->GetCityByList ( $conditions );
			if ($CityList) {
				$html = "<option value=\"0\">请选择</option>";
				
				$this->loadModel ( "User" );
				$UserInfo = $this->User->GetUserByID ( $this->Cookie->read ( 'uid' ) );
				
				foreach ( $CityList as $val ) {
					$is_selected = ($val ['City'] ['id'] == $UserInfo ['User'] ['area']) ? "selected=\"selected\"" : "";
					$html .= "<option {$is_selected} value=\"{$val['City']['id']}\">{$val['City']['name']}</option>";
				}
				echo $html;
				exit ();
			} else {
				echo "<option value=\"0\">请选择</option>";
				exit ();
			}
		}
	}
}
?>